package com.ctshk.app.system.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.rpc.system.dto.*;
import com.ctshk.rpc.system.req.*;
import com.ctshk.rpc.system.service.IMainDataTableService;
import com.ctshk.rpc.system.service.ISystemMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 基础数据模板表 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2020-12-29
 */
@Api(value = "主数据表名称", tags = "主数据表名称管理")
@RestController
@RequestMapping("/main/data/table")
public class MainDataTableController extends BaseController {

    /**
     * 主数据表服务
     */
    @DubboReference
    private IMainDataTableService mainDataTableService;

    @ApiOperation(value = "新增主数据表", notes = "新增主数据表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增系统消息", operDesc = "新增系统消息")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid MainDataTableReq mainDataTableReq) {
        mainDataTableService.add(mainDataTableReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }


    @ApiOperation(value = "查询主数据表列表", notes = "查询主数据表列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询消息列表", operDesc = "查询消息列表")
    @PostMapping("/query-list")
    public Result<PageResponse<MainDataTableDTO>> queryList(@RequestBody @Valid MainDataTableQueryReq mainDataTableQueryReq) {
        return Result.success(mainDataTableService.queryList(mainDataTableQueryReq, getCurrentUser(getRequest()).getId()));
    }

    @ApiOperation(value = "查询数据列表", notes = "查询数据列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询数据列表", operDesc = "查询数据列表")
    @PostMapping("/query-table")
    public Result<PageResponse<MainDataRowDataDTO>> queryTable(@RequestBody @Valid MainDataQueryReq mainDataQueryReq) {
        return Result.success(mainDataTableService.queryTable(mainDataQueryReq));
    }

    @ApiOperation(value = "查询数据列表", notes = "查询数据列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询数据列表", operDesc = "查询数据列表")
    @PostMapping("/query/table/list")
    public Result<PageResponse<MainDataRowDataListDTO>> queryTableList(@RequestBody @Valid MainDataQueryReq mainDataQueryReq) {
        return Result.success(mainDataTableService.queryTableList(mainDataQueryReq));
    }

    @ApiOperation(value = "查询主数据表和表字段", notes = "查询主数据表和表字段")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询主数据表和表字段", operDesc = "查询主数据表和表字段")
    @GetMapping("/get-table/{id}")
    public Result<MainDataTableDTO> getTable(@PathVariable Long id) {
        return Result.success(mainDataTableService.getTable(id));
    }


    @ApiOperation(value = "禁用启用接口", notes = "禁用启用接口")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "禁用启用接口", operDesc = "禁用启用接口")
    @GetMapping("/update/status/{id}")
    public Result updateStatus(@PathVariable Long id) {
        mainDataTableService.updateStatus(id, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "删除表名称", notes = "删除表名称")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.DELETE, operTitle = "删除表名称", operDesc = "删除表名称")
    @GetMapping("/delete/table/{id}")
    public Result deleteTable(@PathVariable Long id) {
        mainDataTableService.deleteTable(id, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "根据所属模块查询全部数据表名称", notes = "根据所属模块查询全部数据表名称")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "根据所属模块查询全部数据表名称", operDesc = "根据所属模块查询全部数据表名称")
    @GetMapping("/query/module/list")
    public Result<List<MainDataModuleDTO>> queryModuleList() {

        return Result.success(mainDataTableService.queryModuleList());
    }

    @ApiOperation(value = "修改数据表属性", notes = "根据所属模块查询全部数据表名称")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "根据所属模块查询全部数据表名称", operDesc = "根据所属模块查询全部数据表名称")
    @PostMapping("/update/table")
    public Result updateTable(@RequestBody @Valid MainDataTableReq mainDataTableReq) {
        mainDataTableService.updateTable(mainDataTableReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }


    @ApiOperation(value = "内部接口，提供查询主数据接口", notes = "内部接口，提供查询主数据接口")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "内部接口，提供查询主数据接口", operDesc = "内部接口，提供查询主数据接口")
    @GetMapping("/query/data/{id}")
    public Result<List<MainDataRowDTO>> queryData(@PathVariable Long id) {

        return Result.success(mainDataTableService.queryData(id));
    }

    @ApiOperation(value = "内部接口，根据表id和字段id查询值", notes = "内部接口，根据行id和字段id查询值")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "内部接口，根据行id和字段id查询值", operDesc = "内部接口，根据行id和字段id查询值")
    @GetMapping("/query/data/field/{tid}/{fid}")
    public Result<List<MainDataRowFieldDataDTO>> queryDataField(@PathVariable Long tid, @PathVariable Long fid) {

        return Result.success(mainDataTableService.queryDataField(tid,fid));
    }

    @ApiOperation(value = "内部接口，根据表id和字段id查询值", notes = "内部接口，根据行id和字段id查询值,去重主数据")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "内部接口，根据行id和字段id查询值和名称模糊", operDesc = "内部接口，根据行id和字段id查询值")
    @PostMapping("/query/data/field/like/name")
    public Result<List<MainDataRowFieldDataDTO>> queryDataFieldLikeName(@RequestBody @Valid MainDataRowFieldDataReq req) {
        return Result.success(mainDataTableService.queryDataFieldLikeName(req));
    }

    @ApiOperation(value = "内部接口，根据表id和字段id查询值，去重主数据", notes = "内部接口，根据行id和字段id查询值，去重主数据")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "内部接口，根据行id和字段id查询值，去重主数据", operDesc = "内部接口，根据行id和字段id查询值，去重主数据")
    @GetMapping("/group/by/data/field/{tid}/{fid}")
    public Result<List<MainDataRowFieldDataDTO>> groupByDataField(@PathVariable Long tid, @PathVariable Long fid) {

        return Result.success(mainDataTableService.groupByDataField(tid,fid));
    }

//    @ApiOperation(value = "内部接口，根据表id和字段id查询值，去重主数据", notes = "内部接口，根据行id和字段id查询值，去重主数据")
//    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "内部接口，根据行id和字段id查询值，去重主数据", operDesc = "内部接口，根据行id和字段id查询值，去重主数据")
//    @GetMapping("/query/data/row/{rid}/{fid}")
//    public Result<List<MainDataRowFieldDataDTO>> groupByDataField(@PathVariable Long tid, @PathVariable Long fid) {
//
//        return Result.success(mainDataTableService.groupByDataField(tid,fid));
//    }



}
